package com.zretc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.entity.Fans;
import com.zretc.service.AttentionService;
import com.zretc.service.FansService;
import com.zretc.service.impl.AttentionServiceImpl;
import com.zretc.service.impl.FansServiceImpl;
import com.zretc.util.PageInfo;

/**
 * Servlet implementation class FansServlet
 */
@WebServlet("/FansServlet")
@MultipartConfig
public class FansServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private FansService fansService = new FansServiceImpl();
    private AttentionService attentionService = new AttentionServiceImpl();
    Gson gson = new Gson();
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FansServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("existFans".equals(op)) {
			doExistFans(request,response);
		}else if ("addFans".equals(op)) {
			doAddFans(request,response);
		}else if ("deleteFans".equals(op)) {
			doDeleteFans(request,response);
		}else if ("getAttentionAndFansCount".equals(op)) {
			doGetAttentionAndFansCount(request,response);
		}else if ("getAttentionAccount".equals(op)) {
			doGetAttentionAccount(request,response);
		}else if ("getFans".equals(op)) {
			doGetFans(request,response);
		}
		
	}
	
	//获取该用户的粉丝列表
	private void doGetFans(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> map = new HashMap<>();
		String accountId = request.getParameter("accountId");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		map.put("accountId", accountId);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Fans> fansByAccountId = fansService.getFansByAccountId(map);
		String json = gson.toJson(fansByAccountId);
		response.getWriter().write(json);
	}

	//获取关注的用户
	private void doGetAttentionAccount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> map = new HashMap<>();
		String fansId = request.getParameter("fansId");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		map.put("fansId", fansId);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Fans> accountByFansId = fansService.getAccountByFansId(map);
		String json = gson.toJson(accountByFansId);
		response.getWriter().write(json);
	}

	//获取关注和粉丝的数量
	private void doGetAttentionAndFansCount(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Integer> map = new HashMap<String, Integer>();
		String accountId = request.getParameter("accountId");
		int fansCount = fansService.fansCount(Integer.parseInt(accountId));
		int attentionCount = attentionService.attentionCount(Integer.parseInt(accountId));
		map.put("fansCount", fansCount);
		map.put("attentionCount", attentionCount);
		String json = gson.toJson(map);
		response.getWriter().write(json);
	}

	//删除粉
	private void doDeleteFans(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fansId = request.getParameter("fansId");
		String beFansId = request.getParameter("beFansId");
		int deleteFans = fansService.deleteFans(Integer.parseInt(beFansId), Integer.parseInt(fansId));
		if (deleteFans>0) {
			response.getWriter().write("success");
		}else {
			response.getWriter().write("no");
		}
	}

	//加粉
	private void doAddFans(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fansId = request.getParameter("fansId");
		String beFansId = request.getParameter("beFansId");
		int addFans = fansService.addFans(Integer.parseInt(beFansId), Integer.parseInt(fansId));
		if (addFans>0) {
			response.getWriter().write("success");
		}else {
			response.getWriter().write("no");
		}
	}

	//是否粉过对方
	private void doExistFans(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String fansId = request.getParameter("fansId");
		String beFansId = request.getParameter("beFansId");
		Fans fans = fansService.getFans(Integer.parseInt(beFansId), Integer.parseInt(fansId));
		if (fans==null) {
			response.getWriter().write("noFans");
		} else {
			response.getWriter().write("yesFans");
		}
	}

}
